package com.budgetme.activities.elements;

import com.budgetme.ui.fragments.BudgetDetailsFragment;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;


public class BudgetProgressBar2 extends ImageView {
	private static final String TAG = BudgetProgressBar2.class.getName();

	private static final float[] BASE_OUTLINE_OUTER_RADII = new float[] { 6, 6, 6, 6, 6, 6, 6, 6 };
	private static final float[] BASE_OUTLINE_INNER_RADII = new float[] { 3, 3, 3, 3, 3, 3, 3, 3 };
	
	private static final float[] BAR_OUTER_RADII = new float[] { 3, 3, 3, 3, 3, 3, 3, 3 };
	private static final float[] BAR_INNER_RADII = new float[] { 2, 2, 2, 2, 2, 2, 2, 2 };
	
	private static final float[] BAR_OUTER_LEFT_RADII  = new float[] { 3, 3, 0, 0, 0, 0, 3, 3 };
	private static final float[] BAR_OUTER_RIGHT_RADII = new float[] { 0, 0, 3, 3, 3, 3, 0, 0 };
	
	private static final float[] BAR_INNER_LEFT_RADII  = new float[] { 2, 2, 0, 0, 0, 0, 2, 2 };
	private static final float[] BAR_INNER_RIGHT_RADII = new float[] { 0, 0, 2, 2, 2, 2, 0, 0 };
	
	private static final int OUTLINE_BAR_INSET = 4;
	private static final RectF OUTLINE_BAR_INSET_RECT = new RectF(OUTLINE_BAR_INSET, OUTLINE_BAR_INSET, OUTLINE_BAR_INSET, OUTLINE_BAR_INSET);
	
	private static final RoundRectShape BASE_BAR_RECT = new RoundRectShape(BASE_OUTLINE_OUTER_RADII, OUTLINE_BAR_INSET_RECT, BASE_OUTLINE_INNER_RADII);
	private static final RoundRectShape PROGRESS_BAR_RECT = new RoundRectShape(BAR_INNER_RADII, null, null);
	private static final RoundRectShape DIFF_BAR_POSITIVE_RECT = new RoundRectShape(BAR_OUTER_RIGHT_RADII, new RectF(6, 6, 6, 6), BAR_INNER_RIGHT_RADII);
	private static final RoundRectShape DIFF_BAR_POSITIVE_LEFT_RECT = new RoundRectShape(BAR_OUTER_RADII, OUTLINE_BAR_INSET_RECT, BAR_INNER_RADII);
	private static final RoundRectShape DIFF_BAR_NEGATIVE_RECT = new RoundRectShape(BAR_OUTER_RIGHT_RADII, null, null);
	
	private static final int HOLO_BLUE_DARK = Color.parseColor("#ff0099cc");
	private static final int HOLO_GREEN_LIGHT = Color.parseColor("#FF99cc00");;
	private static final int HOLO_RED_LIGHT = Color.parseColor("#ffff4444");;

	
	private Paint mPaint;
	private Context mContext;
	
	private int mBaseColor;
	private int mProgressColor;
	private int mDiffColor;
	
	private int mViewHeight;
	private int mViewWidth;
	
	private int mBarPadding = 3;
	

	
	private double mMaxProgress = 0;
	private double mBaselineProgress = 0;
	private double mActualProgress = 0;
	
	private int mMaxProgressNormalized;
	private int mBaselineProgressNormalized;
	private int mActualProgressNormalized;
	
	private Rect mBaseBarBounds = new Rect();
	private Rect mProgressBarBounds = new Rect();
	private Rect mDiffBarBounds = new Rect();
	
	private ShapeDrawable mBaseBarShape = new ShapeDrawable(BASE_BAR_RECT);
	private ShapeDrawable mProgressBarShape = new ShapeDrawable(PROGRESS_BAR_RECT);
	private ShapeDrawable mDiffBarShape = new ShapeDrawable();

	public BudgetProgressBar2(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		mContext = context;
		
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
//		//mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
//		
		mBaseBarShape.setBounds(mBaseBarBounds);
		mBaseBarShape.getPaint().setColor(HOLO_BLUE_DARK);
		mBaseBarShape.draw(canvas);
		

		mProgressBarShape.setBounds(mProgressBarBounds);
		mProgressBarShape.getPaint().setColor(HOLO_BLUE_DARK);
		mProgressBarShape.draw(canvas);
		
		mDiffBarShape.draw(canvas);

	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

	    mViewHeight = MeasureSpec.getSize(heightMeasureSpec);
	    mViewWidth = MeasureSpec.getSize(widthMeasureSpec);
	    
	    mBaseBarBounds.set(mBarPadding, mBarPadding, mViewWidth - mBarPadding, mViewHeight - mBarPadding);
	    
	    mMaxProgressNormalized = mBaseBarBounds.width();
	    
	    normalize();
	    
	}
	
	private void normalize() {
		
		mBaselineProgressNormalized = getNormalizedProgress(mBaselineProgress);
		mActualProgressNormalized = getNormalizedProgress(mActualProgress);
		
		mProgressBarBounds.set(mBarPadding + OUTLINE_BAR_INSET, mBarPadding + OUTLINE_BAR_INSET, mActualProgressNormalized, mViewHeight - mBarPadding - OUTLINE_BAR_INSET);
		
		
		if (mActualProgressNormalized == 0) {
			mDiffBarBounds.set(mBarPadding + OUTLINE_BAR_INSET, mBarPadding + OUTLINE_BAR_INSET, mBaselineProgressNormalized, mViewHeight - mBarPadding - OUTLINE_BAR_INSET);
			mDiffBarShape.setShape(DIFF_BAR_POSITIVE_LEFT_RECT);
			mDiffBarShape.setBounds(mDiffBarBounds);
			mDiffBarShape.getPaint().setColor(HOLO_GREEN_LIGHT);
		} else if (mBaselineProgressNormalized > mActualProgressNormalized) {
			mDiffBarBounds.set(mActualProgressNormalized, mBarPadding + OUTLINE_BAR_INSET, mBaselineProgressNormalized, mViewHeight - mBarPadding - OUTLINE_BAR_INSET);
			mDiffBarShape.setShape(DIFF_BAR_POSITIVE_RECT);
			mDiffBarShape.setBounds(mDiffBarBounds);
			mDiffBarShape.getPaint().setColor(HOLO_GREEN_LIGHT);
		} else {
			mDiffBarBounds.set(mBaselineProgressNormalized, mBarPadding + OUTLINE_BAR_INSET, mActualProgressNormalized, mViewHeight - mBarPadding - OUTLINE_BAR_INSET);
			mDiffBarShape.setShape(DIFF_BAR_NEGATIVE_RECT);
			mDiffBarShape.setBounds(mDiffBarBounds);
			mDiffBarShape.getPaint().setColor(HOLO_RED_LIGHT);
		}
		

		invalidate();
		
	}
	
	private int getNormalizedProgress(double value) {	
		return (int) (mMaxProgressNormalized * value / mMaxProgress);
	}
	
	public void setProgress(double baseline, double actual) {
		
		mBaselineProgress = baseline;
		mActualProgress = actual;
		
		normalize();
		
	}
	
	public void setMaxProgress(double max) {
		
		mMaxProgress = max;
		
		normalize();
		
	}
	
	

	
	

}
